<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Serve;
use App\Models\Bnnaer;
use App\Handlers\ImageUploadHandler;

class BannerController extends Controller
{
       // 检测用户是否登录，无登录跳转到登录页面
    public function __construct()
    {
       $this->middleware('auth');
    }
    //banner首页
    public function index(){

        //取出所有服务
    	$serve = Serve::get();
        //获取所有banner
    	$banner = Bnnaer::get();
        
    	return view('app.banner.index',compact('serve','banner'));
    }

    //添加banner
    public function create(){

    	$serve = Serve::get();

    	return view('app.banner.add',compact('serve'));
    }

    //接受表单数据
    public function store(Request $request,ImageUploadHandler $upload){

        //获取轮播图数量
        $num = Bnnaer::count();

        if($num >= 5){

            return '做多只能有5张banner';exit;
        }
    	//字段白名单
    	$data = $request->only(['href','commit']);

    	//判断是否存在null数据
    	if(!in_array(null, $data)){

    		if(!empty($request->fileimg)){

    			//banner路径
    			$folder = 'banner';
    			//上传地址
    			$file = $upload->save($request->fileimg,$folder);

    			//赋值列表图路径
    			$data['path'] = $file['path'];
    			//赋值服务详情页
    			$data['href'] = URL('serve/details',$request->href);

    			// dd($data);exit;
    			//数据入库
    			$status = Bnnaer::insertGetId($data);

    			if($status){

    				return redirect('app/banner');

    			}else{

    				return '录入失败';
    			}

    		}else{

    			return '请上传banner';
    		}
			

    	}else{

    		return '请检查轮播图文本';
    	}
    	
    }

    //编辑轮播图
    public function edit($id){

        //取出当前轮播图
        $banner = Bnnaer::find($id);
         //取出所有服务
        $serve = Serve::get();

        return view('app.banner.edit',compact('banner','serve'));
    }

    //接受更新数据
    public function update($id,Request $request,ImageUploadHandler $upload){

            //字段白名单
        $data = $request->only(['href','commit']);

        //判断是否存在null数据
        if(!in_array(null, $data)){

            if(!empty($request->fileimg)){

                //banner路径
                $folder = 'banner';
                //上传地址
                $file = $upload->save($request->fileimg,$folder);

                //赋值列表图路径
                $data['path'] = $file['path'];
                //赋值服务详情页
                $data['href'] = URL('serve/details',$request->href);

                //数据入库
                $status =  Bnnaer::where('id',$id)->update($data);

                if($status){

                    return redirect('app/banner');

                }else{

                    return '录入失败';
                }

            }else{

                 //赋值服务详情页
                $data['href'] = URL('serve/details',$request->href);

                //数据入库
                $status =  Bnnaer::where('id',$id)->update($data);

                if($status){

                    return redirect('app/banner');

                }else{

                    return '录入失败';
                }
            }
            

        }else{

            return '请检查轮播图文本';
        }

        
    }
    //删除轮播图
    public function destroy(Request $request){

        $num = Bnnaer::count();

        if($num <= 3){

            return '至少3张banner';exit;
        }


        $status = Bnnaer::where('id',$request->bid)->delete();

        if($status){

            return back();
        }else{

            return '删除失败';
        }
    }
}
